package action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.jspsmart.upload.Request;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import basepath.BasePath;
import xml.Config;
import xml.ConfigUtil;

public class ConfigSetAction extends ActionSupport {
	
	private int task_interval;
	private int page_count;
	private int time_threshold;
	private int size_lower_limit;
	private int size_topper_limit;
	private boolean teacher_clean_power;
	
	private String feedback;
	HttpServletRequest request = ServletActionContext.getRequest(); 
	
	public String configset() throws ServletException, IOException{
		
		Config config = new Config();
		
		config.setTask_interval(task_interval);
		config.setPage_count(page_count);
		config.setTime_threshold(time_threshold);
		config.setSize_lower_limit(size_lower_limit);
		config.setSize_topper_limit(size_topper_limit);
		config.setTeacher_clean_power(teacher_clean_power);
		
		
		HttpServletRequest request = ServletActionContext.getRequest();
		File f = new File(BasePath.getRealBasePath(request)+"data/xml");
		f.mkdirs();
		
		
		final String configFile = BasePath.getRealBasePath(request)+"date/xml/config.xml";
		
		File file = new File(configFile);
		if(!file.exists()){
			System.out.println(configFile+"00000000000000000000000000000011111111111111111");
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}
		System.out.println(configFile);
		
		ConfigUtil.writeConfig(file, config);
			this.feedback="success";
			System.out.println("success");
			return SUCCESS;
		
	
		

		
		//List<Config> cList = ConfigUtil.parseConfig(file);
		
		/*System.out.println(config.getMinSize());
		System.out.println(config.getMaxSize());
		System.out.println(config.isWritable());*/
		
//		System.out.println(cList.get(0).isWritable());
//		System.out.println(cList.get(0).getName());
//		System.out.println(cList.get(0).getMinSize());
//		System.err.println(cList.get(0).getMaxSize());
		
		
		
	}
	
	

	public int getTask_interval() {
		return task_interval;
	}


	public void setTask_interval(int task_interval) {
		this.task_interval = task_interval;
	}


	public int getPage_count() {
		return page_count;
	}


	public void setPage_count(int page_count) {
		this.page_count = page_count;
	}


	public int getTime_threshold() {
		return time_threshold;
	}


	public void setTime_threshold(int time_threshold) {
		this.time_threshold = time_threshold;
	}


	public int getSize_lower_limit() {
		return size_lower_limit;
	}


	public void setSize_lower_limit(int size_lower_limit) {
		this.size_lower_limit = size_lower_limit;
	}


	public int getSize_topper_limit() {
		return size_topper_limit;
	}


	public void setSize_topper_limit(int size_topper_limit) {
		this.size_topper_limit = size_topper_limit;
	}



	public boolean getTeacher_clean_power() {
		return teacher_clean_power;
	}



	public void setTeacher_clean_power(boolean teacher_clean_power) {
		this.teacher_clean_power = teacher_clean_power;
	}
	
	public String getFeedback() {
		return feedback;
	}



	public void setFeedback(String feedback) {
		this.feedback = feedback;
	}


	

}
